<?php
namespace Think\Upload\Driver;

class Oss
{
    // 根目录
    private $rootPath;
    // 配置文件
    private $config = array(
            'AccessKeyId' => '',
            'AccessKeySecret' => ''
    );

    private $bucket = NULL;
    // oss类
    private $Oss = null;
    // 错误信息
    private $error = null;

    /**
     * 架构函数
     *
     * @param string $root
     *            根路径
     * @param array $config
     *            配置文件
     *
     * @return boolean
     */
    public function __construct ($root, $config)
    {
        $this->config = array_merge($this->config, $config);
        $this->bucket = isset($config['Bucket']) ? $config['Bucket'] : null;
        if (file_exists(__DIR__ . '/Oss/aliyun.php')) {
            require_once __DIR__ . '/Oss/aliyun.php';
            $this->Oss = $this->factory();
        } else {
            $this->error = '找不到OSS库文件！';
            return false;
        }
    }

    private function factory ()
    {
        if (is_object($this->Oss))
            return $this->Oss;
        if (file_exists(__DIR__ . '/Oss/aliyun.php')) {
            require_once __DIR__ . '/Oss/aliyun.php';
            $this->Oss = \Aliyun\OSS\OSSClient::factory($this->config);
            return $this->Oss;
        }
        $this->error = '找不到OSS库文件！';
        return false;
    }

    public function checkRootPath ()
    {
        return true;
    }

    function checkSavePath ($savepath)
    {
        // 由于Object机制自动生成存档目录，此处直接返回true
        return true;
    }
    /**
     * 保存类
     * @param unknown $file
     * @param string $replace
     * @return boolean
     */
    function save ($file, $replace = true)
    {
        //文件名
        dump($file);
       // return true;
        $filename = $this->rootPath . $file['savepath'] .
                 (substr($file['savepath'], - 1, 1) == '/' ? '' : '/') .
                 $file['savename'];
        //去掉路径中的./
//         $filename = substr($filename, 0, 1) == '/' ? substr($filename, 1) : $filename;
//         $filename = substr($filename, 0, 2) == './' ? substr($filename, 2) : $filename;
//         $filename = substr($filename, 0, 3) == '../' ? substr($filename, 3) : $filename;
        $str = substr($filename, 0,1);
        while ($str=='/'||$str=="\\"){
            $filename=substr($filename, 1);
            $str = substr($filename, 0,1);
            dump($filename);
        }
        if (! file_exists($file['tmp_name'])) {
            $this->error = 'File Not Found';
            return false;
        }
        try {
            $rs = $this->Oss->putObject(
                    array(
                            'Bucket' => $this->bucket,
                            'Key' => $filename,
                            'Content' => fopen($file['tmp_name'], 'r'),
                            'ContentLength' => filesize($file['tmp_name'])
                    ));
            if ($rs->getETag()) {
                return true;
            }
        } catch (\Aliyun\OSS\Exceptions\OSSException $ex) {
            $this->error = $ex->getMessage();
            return false;
        } catch (\Aliyun\Common\Exceptions\ClientException $ex) {
            $this->error = $ex->getMessage();
            return false;
        }
        $this->error = '未知错误原因！';
        return false;
    }

    /**
     * 创建目录
     *
     * @param string $savepath
     * @return boolean
     */
    function mkdir ($savepath)
    {
        // 基于OSS的Object结构不需要创建目录，因此直接返回true
        return true;
    }

    /**
     * 获取最后一次上传错误信息
     *
     * @return string 错误信息
     */
    function getError ()
    {
        return $this->error;
    }
}